//
//  LCTimeLineDetailViewController.m
//  YinYueTai
//
//  Created by CongCong on 15-7-22.
//  Copyright (c) 2015年 CongCong. All rights reserved.
//

#import "LCTimeLineDetailViewController.h"

@interface LCTimeLineDetailViewController ()
@property (nonatomic) LCTLDetailModel *detailModel;
@property (nonatomic) UIImageView *imageView;
@end

@implementation LCTimeLineDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCollection];
    [self registerNib];
    [self fetchWebDataWith:kTimeLineDetailHeader body:[NSString stringWithFormat:kTimeLineDetailBody,self.contentID]];
    
}
- (void)registerNib
{
    self.flowLayout.minimumLineSpacing = 10;
    self.flowLayout.minimumInteritemSpacing = 10;
    self.collectionView.frame = CGRectMake(0, 64, kScreenSize.width, kScreenSize.height-64);
    [self.collectionView registerNib:[UINib nibWithNibName:@"LCTimeLineCommonCell" bundle:nil] forCellWithReuseIdentifier:@"LCTimeLineCommonCell"];
}
- (void)initJosnModelWith:(NSDictionary *)dataDict
{
    NSDictionary *dict = dataDict[@"data"];
    self.detailModel = [[LCTLDetailModel alloc]initWithDictionary:dict error:nil];
    [self.collectionView reloadData];
}

#pragma mark - <UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.detailModel.commentlist.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCTimeLineCommonCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCTimeLineCommonCell" forIndexPath:indexPath];
    LCTLDetailCommentListModel *model = self.detailModel.commentlist[indexPath.row];
    [cell showWith:model];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
        LCTLDetailCommentListModel *model = self.detailModel.commentlist[indexPath.row];
        NSString *string = model.content;
        CGSize size = [string sizeWithFont:[UIFont systemFontOfSize:17]];
    return CGSizeMake(kScreenSize.width-20, 50+size.width*size.height/(kScreenSize.width-20)+10);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    //点击事件
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *header;
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"Header" forIndexPath:indexPath];
        
    }
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 40, 40)];
    [image sd_setImageWithURL:[NSURL URLWithString:self.detailModel.userinfo.icon] placeholderImage:[UIImage imageNamed:@"avatar-placeholder"]];
    [header addSubview:image];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(60, 10, 150, 40)];
    label.text = self.detailModel.userinfo.uname;
    [header addSubview:label];
    UILabel *timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenSize.width-170, 10, 150, 40)];
    timeLabel.text = self.detailModel.addtime_f;
    timeLabel.textAlignment = NSTextAlignmentRight;
    [header addSubview:timeLabel];
    
    NSString *webString = self.detailModel.html;
    NSMutableString *textString = [[NSMutableString alloc]init];
    if (webString)
    {
        OCGumboDocument *document = [[OCGumboDocument alloc]initWithHTMLString:webString];
        NSArray *rows = document.body.Query(@"article.typo").find(@"p");
        for (OCGumboElement *obj in rows) {
            [textString appendString:[NSString stringWithFormat:@"%@\n",obj.text()]];
        }
    }
    CGSize size = [textString sizeWithFont:[UIFont systemFontOfSize:17]];
    UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 50, kScreenSize.width-20, 40+size.width*size.height/(kScreenSize.width-20))];
    //contentLabel.adjustsFontSizeToFitWidth = YES;
    contentLabel.text = textString;
    contentLabel.numberOfLines = 0;
    
    [header addSubview:contentLabel];
    if (self.detailModel.imglist.count)
    {
        [header addSubview:self.imageView];
    }

    return header;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    NSString *webString = self.detailModel.html;
    NSMutableString *textString = [[NSMutableString alloc]init];
    if (webString)
    {
        OCGumboDocument *document = [[OCGumboDocument alloc]initWithHTMLString:webString];
        NSArray *rows = document.body.Query(@"article.typo").find(@"p");
        for (OCGumboElement *obj in rows) {
            [textString appendString:[NSString stringWithFormat:@"%@\n",obj.text()]];
        }
    }
    CGSize size = [textString sizeWithFont:[UIFont systemFontOfSize:17]];
    if (self.detailModel.imglist.count) {
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 50, kScreenSize.width-20, 0)];
        LCTLImageModel *model = self.detailModel.imglist[0];
        [imageView sd_setImageWithURL:[NSURL URLWithString:model.imgurl] placeholderImage:[UIImage imageNamed:@"defaultCover"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            imageView.frame = CGRectMake(10, 100+size.width*size.height/(kScreenSize.width-20), kScreenSize.width-20, image.size.height*(kScreenSize.width-20.0)/image.size.width);
        }];
        self.imageView = imageView;

        return CGSizeMake(kScreenSize.width, self.imageView.frame.size.height+100+size.width*size.height/(kScreenSize.width-20));
    }
    
    return CGSizeMake(kScreenSize.width, 100+size.width*size.height/(kScreenSize.width-20));
}








@end
